<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="no_name" >
				<uni-data-checkbox v-model="formData.no_name"
					:localdata="formOptions.no_name_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="types" >
				<uni-data-checkbox v-model="formData.types"
					:localdata="formOptions.types_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="rate" >
				<uni-rate placeholder="评分" type="number" v-model="formData.rate"></uni-rate>
			</uni-forms-item>
			<uni-forms-item name="content" >
				<textarea placeholder="评价内容" @input="binddata('content', $event.detail.value)"
					class="uni-textarea-border" v-model="formData.content"></textarea>
			</uni-forms-item>
			<!-- <uni-forms-item name="gs_id" label="">
        <uni-easyinput placeholder="公司id" v-model="formData.gs_id"></uni-easyinput>
      </uni-forms-item> -->
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/pj.js';

	const db = uniCloud.database();
	const dbCollectionName = 'pj';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"no_name": 0,
				"types": 1,
				"rate": 0,
				"content": "",
				"gs_id": ""
			}
			return {
				_id: '',
				formData,
				formOptions: {
					"no_name_localdata": [
						{
							"text": "匿名",
							"value": 0
						},{
							"text": "实名",
							"value": 1
						},
						
					],
					"types_localdata": [{
							"text": "公司环境",
							"value": 1
						},
						{
							"text": "人际关系",
							"value": 2
						},
						{
							"text": "加班情况",
							"value": 3
						},
						{
							"text": "工作难度",
							"value": 4
						},
						{
							"text": "其他",
							"value": 5
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			this._id = e.id
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					res.gs_id = this._id;
					// console.log(res,'这是热水')
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			async submitForm(value) {
				
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then(async (res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					// console.log(value)
					let count = await db.collection('pj').where({
						'gs_id': value.gs_id
					}).get();
					let list = count.result.data;
					let rate = 0
					// console.log(list)
					this.formOptions.types_localdata.forEach(i => {
						// console.log(i.value)
						let listType =  list.filter(item => item.types == i.value);
						let count= listType.length;
						let bigcount = 0;
						// console.log(listType,'jieguo')
						listType.forEach(item => {
						
							// console.log(item)
							if (item.rate > 2) {
								bigcount++
							}
						})
						// console.log(bigcount,count,'对比')
						if (bigcount > (count / 2)) {
							// await db.collection('gongsi').doc(value.gs_id).
							rate++
						}
					})
					// alert(rate)
					// return;
					await db.collection('gongsi').doc(value.gs_id).update({
						rata:rate
					})
					
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>